<?php
class PHPTemplate{
    var $variables  = array();
    
    /**
     * Displays the specified template.
     *
     * @param string $filename
     */
    function display($filename)
    {
        echo $this->fetch($filename);
    }
    
    /**
     * Parses the specified template and returns its content.
     *
     * @param   string $___filename
     * @return  string
     */
    function fetch($___filename)
    {
        extract($this->variables, EXTR_REFS | EXTR_OVERWRITE);
        require_once(SITETEMPLATEDIR.$___filename);
    }
    
    /**
     * Gets the specified assigned variable.
     *
     * @param   string $name
     * @return  mixed
     */
    function get($name)
    {
        return $this->variables[$name];
    }
    
    /**
     * Assigns the variable $name.
     *
     * @param string    $name 
     * @param mixed     $value
     */
    function add($name, $value)
    {
        $this->variables[$name] = $value;
    }
    
    /**
     * Clears the specified variable.
     *
     * @param   string  $name
     */
    function clear($name)
    {
        unset($this->variables[$name]);
    }
    
    /**
     * Clears all variables.
     */
    function clearAll()
    {
        $this->variables = array();
    }
    
    /**
     * Assigns the specified variable $name with the content of $filename.
     *
     * @param   string  $name
     * @param   string  $filename
     * @return  string
     */
    function load($name, $filename)
    {
        if (!is_file(SITETEMPLATEDIR.$filename)) { $this->variables[$name] = "<strong>{$filename}</strong> not found."; return; }
        $content = $this->fetch($filename);
        $this->variables[$name] = $content;
        return $content;
    }
}
?>